//
//  XLVisitorView.swift
//  SinaWeibo
//
//  Created by 李凯 on 16/9/1.
//  Copyright © 2016年 kevin. All rights reserved.
//

import UIKit

class XLVisitorView: UIView {
    
    var block: (() -> ())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        //设置背景颜色
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        //添加控件到视图
        addSubview(iconView)
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //添加约束
        //房子视图
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        //转圈view
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        //消息label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(224)
        }
        
        // 注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        // 登录按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerBtn)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        maskIconView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(registerBtn)
        }
        
    }
    
    //提供一个方法, 供外界设置不同的访客图片
    func setVisitorView (imageName: String?, message: String?) {
        //因为已经设置了默认的访客图片, 因此首页不需要传入图片名称
        if imageName != nil {
            circleView.isHidden = true
            iconView.image = UIImage(named: imageName!)
            messageLabel.text = message
        } else{
            messageLabel.text = message
            startAnimation()
        }
    }
    
    //开启动画
    func startAnimation() {
        //初始化一个动画
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        //设置到某个值
        ani.toValue = M_PI * 2
        //设置重复次数
        ani.repeatCount = MAXFLOAT
        //设置执行一次的事件
        ani.duration = 20
        ani.isRemovedOnCompletion = false
        //将动画添加到circleView上
        circleView.layer.add(ani, forKey: nil)
        
    }
    
    //懒加载控件
    //房子的图标
    lazy var iconView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    //转圈的图标
    lazy var circleView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //消息label
    lazy var messageLabel : UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 14)
        label.text = "哈哈哈哈哈哈哈"
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    //注册按钮
    lazy var registerBtn : UIButton = {
        let btn = UIButton(textColor: UIColor.orange, fontSize: 14)
        btn.setTitle("注册", for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    //登录按钮
    lazy var loginBtn : UIButton = {
        let btn = UIButton(textColor: UIColor.darkGray, fontSize: 14)
        //登录按钮添加点击事件
        btn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        btn.setTitle("登录", for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    //半透明视图
    lazy var maskIconView : UIView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    //设置登录按钮的点击事件
    func loginBtnClick() {
        block?()
    }
    
}
